﻿
//Utility, contains some useful methods.
LightScript.using("LightScript.Core", function()
{
    var thisClass = LightScript.Core.createClass("LightScript.Utility");

    //# Private Members

    //# Constructions

    //# Public Members

    //# Private Methods

    //# Public Methods
    // Gets the pseudo hashcode of a string.
    //str<String>
    //memo: model: unicode^2*(index/length)^4 (1<=index<=length)
    thisClass.getHashCode = getHashCode;
    function getHashCode(str)
    {
        var h = 0, i = len = str.length;
        while (i > 0)
        {
            h += Math.pow(str.charCodeAt(i - 1), 2) * Math.pow(i / len, 4); 
            i--; 
        }
        if (h < 100000)
            h *= 1000;

        return Math.floor(h) % 2147483647;
    }

    thisClass.createRandomString = createRandomString;
    function createRandomString(len)
    {
        var p = [], c;
        for (var i = 0; i < len; i++)
        {
            c = String.fromCharCode(Math.random() * 100);
            if (c.match(/[0-9a-zA-Z]/)) p.push(c);
            else i--;
        }

        return p.join("");
    }

    thisClass.getIPNumber = getIPNumber;
    function getIPNumber(s)
    {
        s = s.split(".");
        return (s[0]++) * 16777216 + (s[1]++) * 65536 + (s[2]++) * 256 + (s[3]++);
    }

    thisClass.getIPString = getIPString;
    function getIPString(ip)
    {
        var str = (ip >>> 24) + "." + (ip << 8 >>> 24) + "." + (ip << 16 >>> 24) + "." + (ip << 24 >>> 24);
        return str;
    }

    //# Shortcuts
});
